package com.ccqlk.lightpointrelation.controller;

import com.ccqlk.lightpointrelation.domain.BoxInfo;
import com.ccqlk.lightpointrelation.domain.LightPoleInfo;
import com.ccqlk.lightpointrelation.domain.LightPointInfo;
import com.ccqlk.lightpointrelation.service.BoxInfoService;
import com.ccqlk.lightpointrelation.service.LightPoleInfoService;
import com.ccqlk.lightpointrelation.service.LightPointInfoService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/box")
public class BoxController {
    @Resource
    private BoxInfoService boxInfoService;
    @Resource
    private LightPoleInfoService lightPoleInfoService;
    @Resource
    private LightPointInfoService lightPointInfoService;

    @PostMapping("/create")
    public String createBox(@RequestBody BoxInfo box) {
        boolean exists = boxInfoService.lambdaQuery().eq(BoxInfo::getCodeNew, box.getCodeNew()).exists();
        if(exists) {
            box.setUpdateTime(new Date());
        } else {
            box.setCreateTime(new Date());
        }
        boolean save = boxInfoService.saveOrUpdate(box);
        return Objects.toString(save);
    }

    @PostMapping("/relation")
    public String relationPole(@RequestBody LightPoleInfo lightPoleInfo) {
        long currentTimeMillis = System.currentTimeMillis();
        boolean exists = lightPoleInfoService.lambdaQuery()
                .eq(LightPoleInfo::getId,lightPoleInfo.getId())
                .eq(LightPoleInfo::getBoxInfoControlCodeNew, lightPoleInfo.getBoxInfoControlCodeNew())
                .exists();
        if(exists){
            return "false";
        }

        lightPoleInfo.setCreateTime(new Date());
        boolean save = lightPoleInfoService.saveOrUpdate(lightPoleInfo);
        System.out.println(System.currentTimeMillis()-currentTimeMillis);
        return Objects.toString(save);
    }

    @PostMapping("/relation-list/{controlCodeNew}")
    public List<LightPoleInfo> listRelationByBoxControlCode(@PathVariable String controlCodeNew) {
        return lightPoleInfoService.lambdaQuery().eq(LightPoleInfo::getBoxInfoControlCodeNew,controlCodeNew).orderByAsc(LightPoleInfo::getLightPoleCode).list();
    }

    @PostMapping("/relation-point")
    public String relationPole(@RequestBody LightPointInfo lightPointInfo) {
        boolean exists = lightPointInfoService.lambdaQuery()
                .eq(LightPointInfo::getBoxInfoControlCodeNew,lightPointInfo.getBoxInfoControlCodeNew())
                .eq(LightPointInfo::get管线点编号, lightPointInfo.get管线点编号())
                .exists();
        if(exists){
            return "false";
        }

        boolean save = lightPointInfoService.saveOrUpdate(lightPointInfo);
        return Objects.toString(save);
    }

    @PostMapping("/relation-point-list/{controlCodeNew}")
    public List<LightPointInfo> listRelationPointByBoxControlCode(@PathVariable String controlCodeNew) {
        return lightPointInfoService.lambdaQuery().eq(LightPointInfo::getBoxInfoControlCodeNew,controlCodeNew).orderByAsc(LightPointInfo::getId).list();
    }

    @DeleteMapping("/relation-delete/{deleteId}")
    public Boolean relationDelete(@PathVariable Long deleteId) {
        return lightPoleInfoService.removeById(deleteId);
    }

    @DeleteMapping("/relation-point-delete/{deleteId}")
    public Boolean relationPointDelete(@PathVariable Long deleteId) {
        return lightPointInfoService.removeById(deleteId);
    }
}
